package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.AddressBookService;
import com.sky.service.ShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    AddressBookMapper addressBookMapper;
    @Override
    public List<AddressBook> list() {
       List<AddressBook> list=addressBookMapper.list(BaseContext.getCurrentId());
        return list;
    }

    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook=addressBookMapper.getById(id);
        return addressBook;
    }

    @Override
    public AddressBook getDefault() {

        AddressBook addressBook=addressBookMapper.getDefault(BaseContext.getCurrentId());
        return addressBook;
    }

    @Override
    public void save(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.save(addressBook);
    }

    @Override
    public void setDefault(Long id) {
        AddressBook aDefault = addressBookMapper.getDefault(BaseContext.getCurrentId());
        if(aDefault!=null){
            addressBookMapper.setDefault(aDefault.getId(),BaseContext.getCurrentId(),0);
        }
        addressBookMapper.setDefault(id,BaseContext.getCurrentId(),1);

    }

    @Override
    public void update(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.update(addressBook);
    }

    @Override
    public void delete(Long id) {
        addressBookMapper.delete(id,BaseContext.getCurrentId());
    }
}
